
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/generated/locales.g.dart';

typedef PickerCallback = Function(int index, dynamic value);

class SinglePicker{

  static showPicker(BuildContext context, title, List dataList, dynamic value, PickerCallback callback) {
    var index = 0;
    for (var i = 0; i < dataList.length; i++) {
      if (value == dataList[i]) {
        index = i;
        break;
      }
    }
    var controller = FixedExtentScrollController(initialItem: index);
    var selectedValue = value;
    var selectedIndex = 0;

    return showModalBottomSheet<int>(
      backgroundColor: Colors.black,
      isScrollControlled: true,
      context: context,
      builder: (BuildContext context) {
        return Container(
          clipBehavior: Clip.antiAlias,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(40.r),
                topRight: Radius.circular(40.r)),
          ),
          height: 834.h,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
                height: 150.h,
                child: Stack(
                  textDirection: TextDirection.rtl,
                  children: [
                    Center(
                        child: Text(title, style: TextStyle(fontSize: 54.sp))),
                  ],
                ),
              ),
              Expanded(
                child: CupertinoPicker(
                  scrollController: controller,
                  diameterRatio: 1,
                  magnification: 1.3,
                  itemExtent: 100.h,
                  squeeze: 0.6,
                  onSelectedItemChanged: (index) {
                    selectedIndex = index;
                    selectedValue = dataList[index];
                  },
                  children: dataList.map((data) {
                    return Center(
                      child: Text(data.toString()),
                    );
                  }).toList(),
                ),
              ),
              SizedBox(
                height: 150.h,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    GestureDetector(
                      child: Text(LocaleKeys.cancel.ts,
                          style: TextStyle(fontSize: 42.sp)),
                      onTap: () => Get.back(),
                    ),
                    GestureDetector(
                      child: Text(LocaleKeys.ok.ts,
                          style: TextStyle(fontSize: 42.sp)),
                      onTap: () {
                        callback(selectedIndex, selectedValue);
                        Get.back();
                      },
                    ),
                  ],
                ),
              )
            ],
          ),
        );
      },
    );
  }
}